// 
//  Copyright 2011  reda
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
using System;
using ManiacMusicManager.Data.Core;
using NHibernate;
using ManiacMusicManager.Data.DAO.Ifaces;
using System.Collections.Generic;
using FluentNHibernate.Conventions;
using System.Linq;
using System.Globalization;
namespace ManiacMusicManager.Data.DAO
{
	public class GenreDAO : AbstractDAO<Genre>, IGenreDAO
	{
		public GenreDAO (ISession session)
			: base(session)
		{
		}
	
		
		#region Convention
		private static readonly string[] convention = new string[] {
			"Blues", "Classic rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies",
			"Autre", "Pop", "R'n'B", "Rap", "Reggae", "Rock", "Techno", "Musique industrielle (Industrial)", "Rock alternatif (Alternative)",
			"Ska", "Death metal", "Pranks", "Musique de film (Film score)2", "Euro-Techno", "Ambient", "Trip-hop", "Musique vocale (Vocal)",
			"Jazz-Funk", "Fusion", "Trance", "Musique classique (Classical)", "Instrumental", "Acid", "House", "Musique de jeu vidéo",
			"Extrait sonore (Sound Clip ou Sample)", "Gospel", "Musique bruitiste (Noise)", "Rock alternatif", "Bass", "Soul", "Punk",
			"Space", "Musique de médiation (Meditative)", "Pop instrumental", "Rock instrumental", "Musique ethnique", "Gothique",
			"Darkwave", "Techno-Industrial", "Musique électronique 3", "Pop-Folk", "Eurodance", "Dream", "Rock sudiste (Southern Rock)",
			"Comédie", "Morceau 'culte' (Cult)", "Gangsta", "Hit-parade (Top 40)", "Rap chrétien (Christian Rap)", "Pop/Funk", "Jungle",
			"Musique amérindienne 1", "Cabaret", "New wave", "Psychédélique", "Rave", "Comédie musicale (Showtunes)", "Bande-annonce",
			"Lo-fi", "Musique tribale", "Acid Punk", "Acid Jazz", "Polka", "Rétro", "Théâtre", "Rock & Roll", "Hard Rock", "Folk",
			"Folk rock", "Folk américain", "Swing", "Fast Fusion", "Bebop", "Musique latine", "Revival", "Musique celtique", "Bluegrass",
			"Avantgarde", "Gothic Rock", "Rock progressif", "Rock psychédélique", "Rock symphonique", "Slow Rock", "Big Band", "Chœur",
			"Easy listening", "Acoustic (Acoustique)", "Humour", "Discours (Speech)", "Chanson", "Opéra", "Musique de chambre", "Sonate",
			"Symphonie", "Booty Bass", "Primus[Quoi ?]", "Porn groove", "Satire", "R'n'B contemporain ou Slow Jam", "Club", "Tango", "Samba",
			"Folklore", "Ballade", "Power ballad", "Rythmic soul", "Freestyle", "Duo", "Punk rock", "Drum Solo (solo de batterie)",
			"A cappella", "Euro-house", "Dancehall", "Goa", "Drum and bass", "Club House", "Hardcore", "Terror", "Indie", "BritPop",
			"Negerpunk", "Polsk Punk", "Beat", "Gangsta rap chrétien", "Heavy metal", "Black metal", "Crossover",
			"Musique chrétienne contemporaine", "Rock chrétien", "Merengue", "Salsa", "Thrash metal", "Anime ou Animé", "JPop", "Synthpop"
		};
		#endregion	
		
		#region Private
		private uint PickNewNumber() {
			var rand = new Random();
			var list = (from a in this.Linq() 
				              select a.Number);
			uint i;
			bool ok = false;
			do {
				i = (uint)rand.Next(convention.Length, int.MaxValue);
				ok = !list.Contains(i);
			} while(ok);
			
			return i;
		}
		#endregion
		
		#region IGenreDAO implementation
		public Genre InstanciateByName (string name)
		{
			var obj = this.GetByName(name);
			if(obj != null)
				return obj;
			
			for(uint i = 0; i < convention.Length; i++) {
				var convName = convention[i];
				if(convName.ToLower()==name.ToLower())
					return new Genre() { Number = i, Name = convName };
			}
			
			TextInfo myTi = new CultureInfo("en-US",false).TextInfo;
			return new Genre() { Number = PickNewNumber(), Name = myTi.ToTitleCase(name) };
		}
		#endregion
	}
}

